package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallBalanceRecordMapper;
import org.linlinjava.litemall.db.domain.LitemallBalanceRecord;
import org.linlinjava.litemall.db.domain.LitemallBalanceRecordExample;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallBalanceRecordService {
    @Resource
    private LitemallBalanceRecordMapper mapper;

    /**
     * 创建
     * @param record
     * @return
     */
    @Async
    public void create(LitemallBalanceRecord record) {
        record.setAddTime(LocalDateTime.now());
        record.setUpdateTime(LocalDateTime.now());
        mapper.insertSelective(record);
    }

    /**
     * 列表
     * @param userId
     * @param page
     * @param size
     * @param sort
     * @param order
     * @return
     */
    public List<LitemallBalanceRecord> querySelective(Integer userId, Short type, Integer page, Integer size, String sort, String order) {
        LitemallBalanceRecordExample example = new LitemallBalanceRecordExample();
        LitemallBalanceRecordExample.Criteria criteria = example.createCriteria();
        if (type!=null) {
            criteria.andTypeEqualTo(type);
        }
        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(userId);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return mapper.selectByExample(example);
    }
}
